package main.niit.servelet;

import com.google.gson.Gson;
import main.niit.pojo.User;
import main.niit.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import java.io.PrintStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/getfind")
public class getfindServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");

        UserServiceImpl userService = new UserServiceImpl();
        String name = req.getParameter("name");
        List<User> list =null; // 调用自定义的搜索方法
        try {
            list = userService.getfind(name);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 将结果转换为JSON格式
        Gson gson = new Gson();
        String json = gson.toJson(list);

        System.out.println(name);
        // 设置响应类型为JSON
        resp.getWriter().write(json);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

}
